package com.cos.students.util;

import com.cos.students.common.Const;
import com.cos.students.vo.output.LoginInfoVo;
import io.jsonwebtoken.*;

import java.util.Date;

/**
 * @author: COS
 * @time: 2022/10/26 10:42
 * @description:
 */
public class TokenUtils {

    public static String getToken(LoginInfoVo infoVo, Long times) {
        long l = System.currentTimeMillis();
        //1 生成token 字符串
        return Jwts.builder()
                .claim("type", infoVo.getType())
                .setId(infoVo.getUserId().toString())
                .setSubject(infoVo.getUsername())
                .setIssuedAt(new Date(l))
                .setExpiration(new Date(l + times))
                .signWith(SignatureAlgorithm.HS256, Const.TOKEN_SING)
                .compact();
    }


    public static Claims parseToken(String token) {
        return (Claims) Jwts.parser()
                .setSigningKey(Const.TOKEN_SING)
                .parse(token).getBody();
    }

}
